/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import java.io.Serializable;

import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;


/**
 * This class corresponds to the "map-to" element of the faare_mapping.xml file.
 * This defines a set of LDAP object classes corresponding to the mapped object.
 */
public class JBMapTo implements Serializable
{
    /** Comma separated list of object classes. */
    private String ldapoc;

    /** LDAP filter built from object classes . */
    private String ldapocFilter;

    /** Set of object classes, without "*". */
    private Set ldapocSet = new HashSet();

    /** If object classes contains *, set to false. */
    private boolean fullySpecified = true;

    /**
     * Returns the comma separated list of object classes.
     * @return list of object classes
     */
    public String getLdapoc()
    {
        return ldapoc;
    }

    /**
     * Sets the comma separated list of object classes.
     * @param aldapoc The list of object classes to set
     */
    public void setLdapoc(String aldapoc)
    {
        this.ldapoc = aldapoc;

        // compute the ldapocSet
        StringTokenizer tok = new StringTokenizer(ldapoc, ",");
        String oc = null;
        StringBuffer buf = new StringBuffer();

        while (tok.hasMoreTokens())
        {
            oc = tok.nextToken();

            if (oc != null)
            {
                // should make this case insensitive : transform to lowercase
                oc = oc.trim().toLowerCase();

                if (oc.equals("*"))
                {
                    fullySpecified = false;
                }
                else
                {
                    // not a joker char
                    ldapocSet.add(oc);
                    buf.append("(objectclass=");
                    buf.append(oc);
                    buf.append(")");
                }
            }
        }

        // end while
        ldapocFilter = buf.toString();
    }

    /**
     * Returns the set of object classes.
     * If configured with "*", "*" is not returned in this set.
     * @return set of object classes.
     */
    public Set getLdapocSet()
    {
        return ldapocSet;
    }

    /**
     * Checks whether the mapped object classes contain "*" or not.
     * @return true if they do not contain "*", false if they do.
     */
    public boolean isFullySpecified()
    {
        return fullySpecified;
    }

    /**
     * Gets the LDAP filter built from object classes.
     *
     * @return LDAP filter
     */
    public String getLdapocFilter()
    {
        return ldapocFilter;
    }

    /**
     * Displays the object classes definition.
     *
     * @return String formatted representation of the definition.
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;
        buf.append(System.getProperty("line.separator"));
        buf.append("......ldapoc = ");
        buf.append(ldapoc);
        buf.append(System.getProperty("line.separator"));
        buf.append("......ldapocFilter = ");
        buf.append(ldapocFilter);
        buf.append(System.getProperty("line.separator"));
        buf.append("......fullySpecified = ");
        buf.append(fullySpecified);
        buf.append(System.getProperty("line.separator"));
        buf.append("......ldapocSet = ");
        buf.append(System.getProperty("line.separator"));
        it = ldapocSet.iterator();

        while (it.hasNext())
        {
            buf.append(".........");
            buf.append((String) it.next());
            buf.append(System.getProperty("line.separator"));
        }

        buf.append(System.getProperty("line.separator"));

        return buf.toString();
    }

    // end of toString method
}
